$(function() {
    //初始化专业
    $.initSubject({
        element:'teacher-subject-box',//初始化专业select元素的ID
        context:'teacher-subject-context',//显示专业/科目名字内容ID
        initData:$('#subjectIds').val(),//初始化显示的专业数据，默认不设置
        removeCallback:function(parentId,subjectIds){//删除回调
            resetSubjectIds()
        },
        confirmCallback:function(parentId,subjectIds){//点击确认回调
            resetSubjectIds()
        }
    });

    /**
     * 重置专业ID串
     */
    function resetSubjectIds() {
        var _subjectIds = $.getAllSubjectIds("teacher-subject-context");
        $("input[name=subjectIds]").val(_subjectIds);
    }

    var isQuestion = $("#isQuestion-2").attr("checked");
    var qaTypeId = $('#qaTypeId').val();
    if(isQuestion == 'checked'){
        findQaTypeList();
    }

    $("body").on("ifClicked","#isQuestion-1",function () {
        $("#question-div").hide();
    }).on("ifClicked","#isQuestion-2",function () {
        findQaTypeList();
        $("#question-div").show();
    }).on("change","#accountType",function () {//选择银行卡时显示地址
        var accountType = $("#accountType").val();
        if(!$.isEmptyObject(accountType) && accountType == 3){
            $("#address").show();
        }else {
            $("#address").hide();
        }
    });

    //初始化页面是否显示开户行地址
    var _accountType = $("#accountType").val();
    if(_accountType == 'BANK'){
        $("#address").show();
    }else{
        $("#address").hide();
    }

    //保存教师后返回教师列表
    $('body').on('click','#save-back',function () {
        saveTeacher(true);
    }).on('click','#save-continue',function () {//保存后继续创建
        saveTeacher(false);
    }).on('click','#cancel',function () { //取消操作
        window.location.href = baseloaction + "/admin/manage/teacher/listTeacher";
    }).on('click',"#teacherImageBtn",function () {
        //初始化图片上传窗口
        initUpdateFileWin({
            fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
            callback:'uploadTeacherCallback',
            cut:true,//开启剪切
            imgSize:DEFAULT_TEACHER_IMG_SIZE,//剪切上传后生成图片的规格（开启剪切才起使用）
            aspectRatio:1/1//剪切区域的高宽比例（开启剪切才起使用）
        });
    }).on('click',"#selectSysUser",function () {//选择用户
        selectSysUser('radio');
    }).on('click','#reomve-sys-user',function () {//删除关联的用户
        $("#sysUserId").val(0);
        $("#user-div .fa").addClass("hide");
        $("#userName>span").text('如果有需要关联管理用户可以选择关联用户');
    }).on('click',"#remove-image",function () {
        removePageImg();
    })
    ;

    //保存讲师
    function saveTeacher(flag){
        var teacherIdentity = $('input[name="teacherIdentity_1"]:checked').map(function () {
            if (this.value > 0) {
                return this.value;
            }
        }).get().join(',');
        $("#teacherIdentity").val(teacherIdentity);
        var params = $('#teacherForm').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction + "/admin/manage/teacher/saveSysTeacher",
            data:params,
            type:"post",
            dataType:"json",
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                icModale(0,result.message);
                if(result.success){
                    if(flag){
                        window.location.href = baseloaction + "/admin/manage/teacher/listTeacher";
                    }else{
                        window.location.href = baseloaction + "/admin/manage/teacher/initAdd";
                    }
                }
            },
            error:function (error) {
                icModale(0,"系统繁忙,讲师数据保存失败");
            }
        });
    }

    //查询问答分类
    function findQaTypeList() {
        $.ajax({
            url: baseloaction + '/admin/qa/qatype/findQaTypeList',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                var qaTypeList = result.entity;
                if(qaTypeList != null && qaTypeList.length > 0){
                    var _html = '<option value="0">选择问答分类</option>';
                    $.each(qaTypeList, function (index, ele) {
                        if(ele.id == qaTypeId){
                            _html += '<option value="'+ ele.id +'" selected="selected">'+ ele.typeName +'</option>';
                        }else {
                            _html += '<option value="'+ ele.id +'">'+ ele.typeName +'</option>';
                        }
                    })
                    $('select[name=qaTypeId]').html(_html);
                    $('select.custom-select').chosen('.chosen-select');
                }
            },
            error: function (error) {
                icModale(0,'获取问答分类失败');
            }
        })
    }
});

//图片上传回调
function uploadTeacherCallback(result) {
    if(result!=null){
        $("#imageJson").val(encodeURIComponent(JSON.stringify(result)));
        $("#teacher-image").attr("src",result.pcUrlMap.large);
        $("#file-names").text(result.fileName);
        $("#create-time").text(result.createTime);
    }
}
//选择用户回调
function selectUserCallback(user) {
    $("#sysUserId").val(user.id);
    $("#userName>span").text(user.ralName+"/"+user.username);
    $("#user-div .fa").removeClass("hide");
}
